package com.cm309.hospital.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cm309.hospital.dao.DoctorDao;
import com.cm309.hospital.dao.PatientDao;
import com.cm309.hospital.dao.SubjectDao;
import com.cm309.hospital.model.Patient;

@WebServlet("/patientEnq.do")
public class PatientEnq extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
//		super.doGet(req, resp);
		
		List doctors = new ArrayList();
		DoctorDao doctorDao = new DoctorDao();
		doctors = doctorDao.queryAll();
		req.setAttribute("doctors",doctors);
		req.getRequestDispatcher("view/patientEnq.jsp").forward(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String patient_id = req.getParameter("patient_id");
		String id_card_num = req.getParameter("id_card_num");
		
		Patient patient = new Patient();
		PatientDao patientDao = new PatientDao();
		patient = patientDao.querySingle(patient_id);

		
		if (patient!=null && patient.getId_card_num().equals(id_card_num))
		{
			req.setAttribute("patient",patient);
			
			List subjects = new ArrayList();
			SubjectDao subjectDao = new SubjectDao();
			subjects = subjectDao.queryAll();
			req.setAttribute("subjects",subjects);

			List doctors = new ArrayList();
			DoctorDao doctorDao = new DoctorDao();
			doctors = doctorDao.queryAll();
			req.setAttribute("doctors",doctors);
			
			req.getRequestDispatcher("view/patientDetail.jsp").forward(req, resp);
		}
		else
		{
			req.getRequestDispatcher("mainPage.do").forward(req,resp);
		}
	}


}
